<?php
class Admini_ExpressController extends Uuch_ManageController {
    
    public function init() {
        parent::init();
        $this->appendCss('/admins/css/yundan.css');
        $this->appendCss('/admins/css/common.css');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
        
        $this->view->type = "Relevance";
    }    
       
    public function indexAction()
    {
        $this->setTitle("快递维护");
        $pagesize = 20;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $name = $this->getSafeParam("name");
        $isVolume = $this->getSafeParam("isVolume");
        $isDisable = $this->getSafeParam("isDisable");
        $isInvoice = $this->getSafeParam("isInvoice");
        $isRemoteFee = $this->getSafeParam("isRemoteFee");
        $expressItem = Application_Model_Express::getExpressFileds($name,$isVolume,$isDisable,$isInvoice,$isRemoteFee,$pagesize,$pageindex);
        $this->view->expressList = $expressItem["list"];
        $this->view->expressAll = $expressItem;
        $this->view->name = $name;
        $this->view->isVolume = $isVolume;
        $this->view->isDisable = $isDisable;
        $this->view->isInvoice = $isInvoice;
        $this->view->isRemoteFee = $isRemoteFee;
        $this->view->type = "Relevance";
        $this->view->stype = 2;
    }

    /**
     * 快递vip列表
    */
    public function viplistAction()
    {
        $this->setTitle("vip价格列表");
        $pagesize = 21;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $name        = $this->getSafeParam("name");
        $isVolume    = $this->getSafeParam("isVolume");
        $isDisable   = $this->getSafeParam("isDisable");
        $isInvoice   = $this->getSafeParam("isInvoice");
        $isRemoteFee = $this->getSafeParam("isRemoteFee");
        $expressItem = Application_Model_Express::getExpressFileds($name,$isVolume,$isDisable,$isInvoice,$isRemoteFee,$pagesize,$pageindex);
        $viplist     = Application_Model_Express::getVip();
        $this->view->expressList = $expressItem["list"];
        $this->view->expressAll  = $expressItem;
        $this->view->name        = $name;
        $this->view->isVolume    = $isVolume;
        $this->view->isDisable   = $isDisable;
        $this->view->isInvoice   = $isInvoice;
        $this->view->isRemoteFee = $isRemoteFee;
        $this->view->type        = "Relevance";
        $this->view->stype       = 6;
        $this->view->viplist     = $viplist;
    }

    /**
     * 清除vip价格设置
    */
    public function vipcleanAction()
    {
        $id = $this->getSafeParam("id");
        if(is_numeric($id)){
            $clean = new Application_Model_ExpressVip();
            $clean->deleteAll(array('expressId' => $id));//删除所有信息
        }
        echo json_encode(array('s' => 0));
        exit;
    }
    
    public function addexpressAction()
    {
        $id = (int)$this->getSafeParam("id");
        $expressItem = Application_Model_Express::load($id);
        if($expressItem){
            $this->setTitle("更新快递");
            
            $arrtype = array();
            if($expressItem->TypeMark == 0){
                $arrtype[0] = true;
            }else{
                foreach(Application_Model_StoreItem::getNames() as $key=>$value){ 
                    $arrtype[$key] = false;
                    if(($expressItem->TypeMark & $key) == $key){
                        $arrtype[$key] = true;
                   }
               }
           }
           $this->view->day         = explode('~', $expressItem->es_time);
           $this->view->arrtype     = $arrtype;         //已选敏感项
           $this->view->expressItem = $expressItem;     //快递
        }else{
            $this->setTitle("添加新快递");
        }

        if($this->isPost()){
            $id             = $this->getSafeParam("id");              //快递id
            $name           = $this->getSafeParam("name");              //快递名称

            $minDay         = (int)$this->getSafeParam("minDay");       //最小时间
            $maxDay         = (int)$this->getSafeParam("maxDay");       //最大时间

            $isRemoteFee    = $this->getSafeParam("isRemoteFee");       //偏远费
            $fuelSurcharge  = $this->getSafeParam("fuel");              //燃油费
            $remoteFee      = $this->getSafeParam("remoteFee");         //偏远费
            $othersFee      = $this->getSafeParam("othersFee");         //其他费用
            $isVolume       = $this->getSafeParam("isVolume");          //是否按体积
            $checkstore = "";
            if(is_array($this->_getParam('checkstore'))){
                foreach ($this->_getParam('checkstore') as $type){
                    $checkstore = $checkstore | intval($type);
                }
            }else{
                $checkstore = $this->_getParam('checkstore');
            }

            $checkstore     = ($checkstore=="" || $checkstore == NULL) ? "0" : $checkstore; //可走敏感项
            
            $isInvoice      = $this->getSafeParam("isInvoice");     //制作发票
            
            $trackType      = $this->getSafeParam("trackType");     //追踪类型
            $imgpath         = $this->getSafeParam("imgpath");          //logo上传

            $webUrl         = $this->getSafeParam("webUrl");        //官网地址
            $trackUrl       = $this->getSafeParam("trackUrl");      //追踪地址
            $helpUrl        = $this->getSafeParam("helpUrl");       //说明地址
            $code        = $this->getSafeParam("code");       //code
            if(is_numeric($id) && $id > 0){
                $expressItem = Application_Model_Express::load($id);
            }else{
                $expressItem = new Application_Model_Express();
            }

            //判断名称是否被占用
            $tepExpress     = Application_Model_Express::getAllbyCondition(array('name' => $name ));
            if(count($tepExpress) > 1 || (count($tepExpress) == 1 && $tepExpress[0]->Id != $id)){
                //错误,名称已经存在
                $this->view->ma = 2;
            }else{
                $expressItem->Name          = $name;
                $expressItem->FuelSurcharge = ((float)$fuelSurcharge)/100;
                $expressItem->RemoteFee     = (float)$remoteFee;
                $expressItem->OthersFee     = (float)$othersFee;
                $expressItem->IsVolume      = $isVolume;
                $expressItem->TypeMark      = $checkstore;
                $expressItem->IsInvoice     = $isInvoice;
                $expressItem->IsRemoteFee   = $isRemoteFee;
                $expressItem->TrackType     = $trackType;
                $expressItem->Logo          = $imgpath;
                $expressItem->WebUrl        = $webUrl;
                $expressItem->TrackUrl      = $trackUrl;
                $expressItem->HelpUrl       = $helpUrl;
                $expressItem->es_time       = $minDay . '~' . $maxDay;
                $expressItem->Code          = $code;
                if(!$expressItem->Id){
                    $expressItem->ReportFee     = 0;            //报关费
                    $expressItem->FirstWeight   = 0;            //首重
                    $expressItem->SubsequentWeight = 0;         //续重
                    $expressItem->Description   = '';           //描叙
                    $expressItem->Remark        = '';           //备注
                    $expressItem->IsDisable     = 1;            //默认禁用
                    $expressItem->IsSpecific    = 0;            //默认非特殊通道
                    $expressItem->MaxWeight     = 0;            //最大重量
                }
                $expressItem->save();

                Application_Model_Operate::addOperate(0, null, $expressItem->Id, "添加(更新)快递：".$expressItem->Name, 0);
                $this->view->ma = 1;
            }
        }
        $this->view->storeArrName = Application_Model_StoreItem::getNames();
    }

    public function enableordisableAction()
    {
        $id = $this->getSafeParam("id");
        $expressItem = Application_Model_Express::load($id);
        if($expressItem){
            if($expressItem->IsDisable == 1){
                $expressItem->IsDisable = 0;
                Application_Model_Operate::addOperate(0, null, $expressItem->Id, "开启快递：".$expressItem->Name, 0);
            } else {
                $expressItem->IsDisable = 1;
                Application_Model_Operate::addOperate(0, null, $expressItem->Id, "禁用快递：".$expressItem->Name, 0);
            }
            $expressItem->save();
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function stateallAction()
    {
        $pagesize=30;        
        $pageindex = $this->getSafeParam('start');
        if(!$pageindex){$pageindex=0;}
        $id = $this->getSafeParam("id");
        $type = $this->getSafeParam("type");
        $expressItem = Application_Model_ExpressPrice::ByExpressIdType($id,$type,$pagesize,$pageindex);
        $this->view->epList = $expressItem["list"];
        $this->view->epAll = $expressItem;
        $this->_helper->layout->disableLayout();
    }


    public function epdetailAction()
    {
        $this->setTitle("快递详情");
        $this->appendCss('/admins/css/adminshare.css');
        $this->appendCss('/admins/css/cangku.css');
        $id = $this->getSafeParam("id");
        $expressItem = Application_Model_Express::load($id);
        if($expressItem){
            if($this->isPost()){
                $name = $this->getSafeParam("name");
                $firstWeight = $this->getSafeParam("firstWeight");
                $subsequentWeight = $this->getSafeParam("subsequentWeight");
                $fuelSurcharge = $this->getSafeParam("fuelSurcharge");
                $isVolume = $this->getSafeParam("isVolume");
                $checkstore = "";
                if(is_array($this->_getParam('checkstore'))){
                    foreach ($this->_getParam('checkstore') as $type){
                        $checkstore = $checkstore | intval($type);
                    }
                }else{
                    $checkstore = $this->_getParam('checkstore');
                }
                $checkstore = ($checkstore=="" || $checkstore == NULL)?0:$checkstore;
                $isDisable = $this->getSafeParam("isDisable");
                $isInvoice = $this->getSafeParam("isInvoice");
                $maxWeight = $this->getSafeParam("maxWeight");
                $isRemoteFee = $this->getSafeParam("isRemoteFee");
                $trackNo = $this->getSafeParam("trackNo");
                $isSpecific = $this->getSafeParam("isSpecific");
                $trackType = $this->getSafeParam("trackType");
                $logo = $this->getSafeParam("logo");
                $webUrl = $this->getSafeParam("webUrl");
                $trackUrl= $this->getSafeParam("trackUrl");
                $helpUrl = $this->getSafeParam("helpUrl");
                $description = $this->getSafeParam("description");
                $remark = $this->getSafeParam("remark");

                $expressItem->Name = $name;
                $expressItem->FirstWeight = $firstWeight;
                $expressItem->SubsequentWeight = $subsequentWeight;
                $expressItem->FuelSurcharge = $fuelSurcharge;
                $expressItem->IsVolume = $isVolume;
                $expressItem->TypeMark = $checkstore;
                $expressItem->IsDisable = $isDisable;
                $expressItem->IsInvoice = $isInvoice;
                $expressItem->MaxWeight = $maxWeight;
                $expressItem->IsRemoteFee = $isRemoteFee;
                $expressItem->TrackNo = $trackNo;
                $expressItem->IsSpecific = $isSpecific;
                $expressItem->TrackType = $trackType;
                $expressItem->Logo = $logo;
                $expressItem->WebUrl = $webUrl;
                $expressItem->TrackUrl = $trackUrl;
                $expressItem->HelpUrl = $helpUrl;
                $expressItem->Description = $description;
                $expressItem->Remark = $remark;
                $expressItem->save();
                Application_Model_Operate::addOperate(0, null, $expressItem->Id, "修改快递：".$expressItem->Name, 0);
                $this->view->ma = 1;
            }   
        } else {
            $this->view->ma = 2;
        }
        $this->view->storeArrName = Application_Model_StoreItem::getNames();
        $this->view->expressItem = $expressItem;
        $this->view->stype = 2;
    }
    
    public function priceAction()
    {
        $this->setTitle("快递价格维护");
        $pagesize = 20;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $epId = $this->getSafeParam("epId");
        $cityId = $this->getSafeParam("cityId");
        $isDisable = $this->getSafeParam("isDisable");
        $priceItem = Application_Model_ExpressPrice::getPriceFilteds($epId,$cityId,$isDisable,$pagesize,$pageindex);
        $this->view->priceList = $priceItem["list"];
        $this->view->priceAll = $priceItem;
        $this->view->expresscomp = Application_Model_Express::getAll();
        $words = array();
        for ($i = 0; $i < 26; $i++) {
            $words[] = chr(65 + $i);
        }
        $this->view->words = $words;
        $this->view->epId = $epId;
        $this->view->cityId = $cityId;
        $this->view->isDisable = $isDisable;
        $this->view->type = "Relevance";
        $this->view->stype = 3;
    }
    
    public function prdetailAction()
    {
        $this->setTitle("快递价格详情");
        $this->appendCss('/admins/css/adminshare.css');
        $this->appendCss('/admins/css/cangku.css');
        $id = $this->getSafeParam("id");
        $priceItem = Application_Model_ExpressPrice::load($id);
        if(!$priceItem){
            $this->_redirect('admini/express/price');
        }
        // if($priceItem){
        //     if($this->isPost()){
        //         $epId = $this->getSafeParam("epId");
        //         $cityId = $this->getSafeParam("cityId");
        //         $firstWeight = $this->getSafeParam("firstWeight");
        //         $subsequentWeight = $this->getSafeParam("subsequentWeight");
        //         $firstPrice = $this->getSafeParam("firstPrice");
        //         $subsequentPrice = $this->getSafeParam("subsequentPrice");
        //         $isDisable = $this->getSafeParam("isDisable");
        //         $mintime = $this->getSafeParam("mintime");
        //         $maxtime = $this->getSafeParam("maxtime");
        //         $firstReport = $this->getSafeParam("firstReport");
        //         $subsequentReport = $this->getSafeParam("subsequentReport");
        //         $price5 = $this->getSafeParam("price5");
        //         $price10 = $this->getSafeParam("price10");
        //         $price20 = $this->getSafeParam("price20");
        //         $price40 = $this->getSafeParam("price40");
        //         $price70 = $this->getSafeParam("price70");
        //         $priceItem->ExpressId = $epId;
        //         $priceItem->CityId = $cityId;
        //         $priceItem->FirstWeight = $firstWeight;
        //         $priceItem->SubsequentWeight = $subsequentWeight;
        //         $priceItem->FirstPrice = number_format($firstPrice,2);
        //         $priceItem->SubsequentPrice = number_format($subsequentPrice,2);
        //         $priceItem->IsDisable = $isDisable;
        //         $priceItem->Times = $mintime."~".$maxtime;
        //         $priceItem->FirstReport = number_format($firstReport,2);
        //         $priceItem->SubsequentReport = number_format($subsequentReport,2);
        //         $priceItem->Price5 = number_format($price5,2);
        //         $priceItem->Price10 = number_format($price10,2);
        //         $priceItem->Price20 = number_format($price20,2);
        //         $priceItem->Price40 = number_format($price40,2);
        //         $priceItem->Price70 = number_format($price70,2);
        //         $priceItem->save();
        //         Application_Model_Operate::addOperate(0, null, $priceItem->Id, "修改快递价格", 0);
        //         $this->view->ma = 1;
        //     }
        // }
        // $this->view->expresscomp = Application_Model_Express::getAll();
        // $words = array();
        // for ($i = 0; $i < 26; $i++) {
        //     $words[] = chr(65 + $i);
        // }
        // $this->view->words = $words;
        $this->view->priceItem = $priceItem;
        $this->view->stype = 3;
    }


    /**
     * 启用或禁止该国家的某渠道
     */
    public function eodpriceAction()
    {
        $id = $this->getSafeParam("id");
        $priceItem = Application_Model_ExpressPrice::load($id);
        if($priceItem){
            if($priceItem->IsDisable == 1){
                $priceItem->IsDisable = 0;
            } else {
                $priceItem->IsDisable = 1;
            }
            $priceItem->save();
            $this->json(1);
        } else {
            $this->json(0);
        }
    }


    /**
     * 批量启用该国家的某渠道
     */
    public function enablepriceAction()
    {
        $idlist = $this->getSafeParam("idlist");
        $arrId = explode(",", $idlist);
        for ($index = 0; $index < count($arrId); $index++) {
            if($arrId[$index] != ""){
                $priceItem = Application_Model_ExpressPrice::load($arrId[$index]);
                if($priceItem->IsDisable == 1){
                    $priceItem->IsDisable = 0;
                } 
                $priceItem->save();
            }
        }
        $this->json(1);
    }
    /**
     * 批量禁止该国家的某渠道
     */
    public function disablepriceAction()
    {
        $idlist = $this->getSafeParam("idlist");
        $arrId = explode(",", $idlist);
        for ($index = 0; $index < count($arrId); $index++) {
            if($arrId[$index] != ""){
                $priceItem = Application_Model_ExpressPrice::load($arrId[$index]);
                if($priceItem->IsDisable == 0){
                    $priceItem->IsDisable = 1;
                } 
                $priceItem->save();
            }
        }
        $this->json(1);
    }
    
    public function companyAction()
    {
        $this->setTitle("渠道商管理");
        $pagesize = 20;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $quick = $this->getSafeParam("quick");
        $isDisable = $this->getSafeParam("isDisable");
        $companyItem = Application_Model_AgentCompany::getPriceFilteds($quick,$isDisable,$pagesize,$pageindex);
        $this->view->companyList = $companyItem["list"];
        $this->view->companyAll = $companyItem;
        $this->view->quick = $quick;
        $this->view->isDisable = $isDisable;
        $this->view->ma = $this->getSafeParam("ma");
        $this->view->type = "Relevance";
        $this->view->stype = 4;
    }
    
    public function enableordisablecpAction()
    {
        $id = $this->getSafeParam("id");
        $companyItem = Application_Model_AgentCompany::load($id);
        if($companyItem){
            if($companyItem->IsDisable == 1){
                $companyItem->IsDisable = 0;
            } else {
                $companyItem->IsDisable = 1;
            }
            $companyItem->save();
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function delexpressAction()
    {
        $id = $this->getSafeParam("id");
        $companyItem = Application_Model_ExpressPrice::load($id);
        if($companyItem){
            $clean = new Application_Model_ExpressPricing();
            $clean->deleteAll(array('expressId' => $companyItem->expressId, 'countryId' => $companyItem->cityId));//删除所有信息
            $companyItem->delete();
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function cpdetailAction()
    {
        $this->setTitle("渠道详情");
        $this->appendCss('/admins/css/adminshare.css');
        $this->appendCss('/admins/css/cangku.css');
        $id = $this->getSafeParam("id");
        $companyItem = Application_Model_AgentCompany::load($id);
        if($companyItem){
            if($this->isPost()){
                $companyItem->Name = $this->getSafeParam("name");
                $companyItem->Linkman = $this->getSafeParam("linkman");
                $companyItem->Tel = $this->getSafeParam("tel");
                $companyItem->Address = $this->getSafeParam("address");
                $companyItem->Url = $this->getSafeParam("url");
                $companyItem->Host = $companyItem->Url;
                $companyItem->Username = $this->getSafeParam("username");
                $companyItem->Password = $this->getSafeParam("password");
                $companyItem->save();
                Application_Model_Operate::addOperate(0, null, $companyItem->Id, "修改渠道商：".$companyItem->Name, 0);
                $this->view->ma = 1;
            }
        }
        $this->view->isEdit = $this->getSafeParam("isEdit");
        $this->view->cpdetail = $companyItem;
    }
    
    public function addcpAction()
    {
        if($this->isPost()){
            $cpItem = new Application_Model_AgentCompany();
            $cpItem->Name = $this->getSafeParam("name");
            $cpItem->Linkman = $this->getSafeParam("linkman");
            $cpItem->Tel = $this->getSafeParam("tel");
            $cpItem->Address = $this->getSafeParam("address");
            $cpItem->Url = $this->getSafeParam("url");
            $cpItem->Host = $cpItem->Url;
            $cpItem->Username = $this->getSafeParam("username");
            $cpItem->Password = $this->getSafeParam("password");
            $cpItem->IsDisable = $this->getSafeParam("isDisable");
            Application_Model_Operate::addOperate(0, null, $cpItem->Id, "添加渠道商：".$cpItem->Name, 0);
            $cpItem->save();
            $this->_redirect('/admini/express/company/ma/1');
        }
        $this->_redirect('/admini/express/company/ma/0');
    }
    
    public function channelAction()
    {
        $this->setTitle("渠道维护");
        $pagesize = 40;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $quick = $this->getSafeParam("quick");
        $expressId = $this->getSafeParam("expressId");
        $actId = $this->getSafeParam("agentId");
        $isShipNo = $this->getSafeParam("isShipNo");
        $channelItem = Application_Model_ExpressChannel::getPriceFilteds($quick,$expressId,$actId,$isShipNo,$pagesize,$pageindex);
        $this->view->channelList = $channelItem["list"];
        $this->view->channelAll = $channelItem;
        $this->view->quick = $quick;
        $this->view->expressId = $expressId;
        $this->view->expressAll = Application_Model_Express::getAll();
        $this->view->agentId = $actId;
        $this->view->acAll = Application_Model_AgentCompany::getAll();
        $this->view->isShipNo = $isShipNo;
        $this->view->type = "Relevance";
        $this->view->stype = 5;
    }
    
    public function channelinfoAction()
    {
        $channelId = $this->getSafeParam("channelId");
        $channelItem = Application_Model_ExpressChannel::load($channelId);
        if($channelItem){
            $this->json(array("type"=>1,"name"=>$channelItem->Name,"key"=>$channelItem->Key,"expressId"=>$channelItem->ExpressId,"agentCompanyId"=>$channelItem->AgentCompanyId,"isShipNo"=>$channelItem->IsShipNo));
        } else {
            $this->json(array("type"=>0));
        }
    }
    
    public function addoreditAction()
    {
        $channelId = $this->getSafeParam("channelId");
        $name = $this->getSafeParam("name");
        $key = $this->getSafeParam("key");
        $expressId = $this->getSafeParam("expressId");
        $agentCompanyId = $this->getSafeParam("agentId");
        $isShipNo = $this->getSafeParam("isShipNo");
        if($channelId){
            $channelItem = Application_Model_ExpressChannel::load($channelId);
            if(!$channelItem){
                $channelItem = new Application_Model_ExpressChannel();
            }
        } else {
            $channelItem = new Application_Model_ExpressChannel();
        }
        $channelItem->Name = $name;
        $channelItem->Key = $key;
        $channelItem->expressId = $expressId;
        $channelItem->agentCompanyId = $agentCompanyId;
        $channelItem->isShipNo = $isShipNo;
        $channelItem->save();
        Application_Model_Operate::addOperate(0, null, $channelItem->Id, "添加后台渠道：".$channelItem->Name, 0);
        $this->json(1);
    }
    
    public function delchannelAction()
    {
        $channelId = $this->getSafeParam("channelId");
        $channelItem = Application_Model_ExpressChannel::load($channelId);
        if($channelItem){
            $channelItem->delete();
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function addpriceAction()
    {
        $epId       = $this->getSafeParam("epId");      //快递id
        $cityId     = $this->getSafeParam("cityId");    //国家id
        $priceInfo  = Application_Model_ExpressPrice::getPriceByExpressAndCity($epId, $cityId);
        if($this->isPost()){
            //续重
            $startx = $_POST['startx']; //重量段
            $endx   = $_POST['endx'];   //重量段
            $pricex = $_POST['pricex']; //续重价
            //按公斤算
            $startg = $_POST['startg']; //重量段
            $endg   = $_POST['endg'];   //重量段
            $priceg = $_POST['priceg']; //每公斤价

            $firstWeight = $this->getSafeParam("firstWeight");              //首重
            $subsequentWeight = $this->getSafeParam("subsequentWeight");    //续重
            $firstPrice = $this->getSafeParam("firstPrice");                //首重价
            $maxkg      = $this->getSafeParam("maxkg");                     //最大限重
            $maxcm      = $this->getSafeParam("maxcm");                     //最大限长
            $beyond     = $this->getSafeParam("beyond");                    //超过按公斤算
            $isWeight   = $this->getSafeParam("isWeight");                  //1按公斤计算

            //添加快递start
            if($priceInfo){
                //做更新
                $priceItem = Application_Model_ExpressPrice::load($priceInfo->Id);
            }else{
                //做添加
                $priceItem = new Application_Model_ExpressPrice();
            }
            $priceItem->ExpressId   = $epId;                        //快递id
            $priceItem->CityId      = $cityId;                      //国家id
            $priceItem->FirstWeight = $firstWeight;                 //首重
            $priceItem->SubsequentWeight = $subsequentWeight;       //续重
            $priceItem->FirstPrice  = number_format($firstPrice,2); //首重价
            $priceItem->Maxkg       = $maxkg;                       //最大限重
            $priceItem->Maxcm       = $maxcm;                       //最大限长
            $priceItem->Beyond      = $beyond;                      //超过按公斤算
            $priceItem->IsWeight    = $isWeight;                    //1按公斤计算

            if(!$priceInfo){
                //新增给默认数据
                $priceItem->SubsequentPrice = 0;
                $priceItem->typeMark    = '';
                $priceItem->IsDisable   = 1;    //禁用
                $priceItem->Times = 0;
                $priceItem->FirstReport = 0;
                $priceItem->SubsequentReport = 0;
                $priceItem->firstFuel        = 0; // 暂未用到 默认
                $priceItem->subsequentFuel   = 0; //暂未用到 默认
                $priceItem->Price5  = 0;    //默认
                $priceItem->Price10 = 0;    //默认
                $priceItem->Price20 = 0;    //默认
                $priceItem->Price40 = 0;    //默认
                $priceItem->Price70 = 0;    //默认
            }
            $priceItem->save();
            //添加快递end

            $clean = new Application_Model_ExpressPricing();
            $clean->deleteAll(array('expressId' => $epId, 'countryId' => $cityId));//删除所有信息
            //续重
            foreach ($startx as $key => $value) {
                $Pricing = new Application_Model_ExpressPricing();
                $Pricing->ExpressId = $epId;
                $Pricing->CountryId = $cityId;
                $Pricing->Type      = Application_Model_ExpressPricing::TYPE_CONTINUED;
                $Pricing->Start     = $value;
                $Pricing->End       = $endx[$key];
                $Pricing->Price     = $pricex[$key];
                $Pricing->save();
            }

            if($isWeight == 1){
                //按公斤算
                foreach ($startg as $key => $value) {
                    $Pricing = new Application_Model_ExpressPricing();
                    $Pricing->ExpressId = $epId;
                    $Pricing->CountryId = $cityId;
                    $Pricing->Type      = Application_Model_ExpressPricing::TYPE_KG;
                    $Pricing->Start     = $value;
                    $Pricing->End       = $endg[$key];
                    $Pricing->Price     = $priceg[$key];
                    $Pricing->save();
                }
            }

            Application_Model_Operate::addOperate(0, null, $priceItem->Id, "添加(更新)前台快递价格", 0);
            $this->view->ma = 1;
            $this->view->priceId = $priceItem->Id;
        }
        $this->view->expresscomp = Application_Model_Express::getAll();
        $words = array();
        for ($i = 0; $i < 26; $i++) {
            $words[] = chr(65 + $i);
        }
        $this->view->epId = $epId;
        $this->view->cityId = $cityId;
        $this->view->priceInfo = $priceInfo;
        $this->view->words = $words;
        $this->view->stype = 3;
    }
    
    public function downtemplateAction()
    {
        $er = new Application_Model_Filter_GetdataDump();
        $er->title = "批量模版";
        $er->printHeadArr = array('国家名称(提示用)', '渠道ID', '国家ID', '首重(g)', '续重(g)', '首重价', '是否禁用1禁用0启用', '最大限重(kg)', '最大限长(cm)', '超过按公斤算', '0关闭1开启按公斤计算', '0~10', '10~15', '15~20', 'kg20~30', 'kg30~100');
        $er->dumpDataTop();
        exit ;
    }

    public function downcityallAction()
    {
        $er = new Application_Model_Filter_GetdataDump();
        $er->title = "国家详情";
        $er->printHeadArr = array("国家ID","国家名称");
        $er->printTypeArr = array("id","name");
        $er->dumpDataTop();
        $cityItem = Application_Model_city::byAllcity(array('parentId' => 0), 'id');
        $er->dumpObjectData($cityItem);
        exit ;
    }

    //下载启用的渠道列表
    public function downexpressallAction()
    {
        $er = new Application_Model_Filter_GetdataDump();
        $er->title = "启用的渠道列表";
        $er->printHeadArr = array("渠道ID","渠道名称");
        $er->printTypeArr = array("id","name");
        $er->dumpDataTop();
        $item = Application_Model_Express::getByCondition(array("isDisable" => 0, ));
        $er->dumpObjectData($item);
        exit ;
    }
    
    public function downtypeallAction()
    {
        $er = new Application_Model_Filter_GetdataDump();
        $er->title = "货物编码";
        $er->printHeadArr = array("类型编码","类型名称");
        $er->dumpDataTop();
        $storeTyleAll = Application_Model_StoreItem::getNames();
        $dataArr = array();
        foreach ($storeTyleAll as $typeKey => $typeVal) {
            $dataArr[$typeKey] = $typeVal[0];
        }
        $er->dumoArrData($dataArr);
        exit ;
    }

    //价格区间模板
    public function downpricingAction()
    {
        $er = new Application_Model_Filter_GetdataDump();
        $er->title = "价格区间模板";
        $er->printHeadArr = array("渠道ID", "国家ID", "类型(1续重2公斤)", "开始重量段", "结束重量段", "价格");
        $er->dumpDataTop();
        exit ;
    }

    public function batchupAction()
    {
        if($this->isPost()){
            $updateLog = "";
            if(!($_FILES["expressPriceFile"]["type"] == "application/vnd.ms-excel" || $_FILES["expressPriceFile"]["type"] == "application/vnd.ms-execl" )){ 
                $this->view->updateLog = "<font color='#c00;'>文件格式错误，只处理 CSV 文件。</font>";
                return ;
            }
            if(pathinfo($_FILES["expressPriceFile"]["name"], PATHINFO_EXTENSION) == "csv"){
                //一次导入
                $updateLog = $this->expressCsvnew($_FILES['expressPriceFile']['tmp_name']);
            } else {
                $updateLog .= "<font color='#c00;'>文件格式错误，只处理 CSV 文件。</font>";
            }
            
            $this->view->updateLog = $updateLog;
        }
        $this->view->stype = 3;
    }
    //一次导入更新
    protected function expressCsvnew($fileUrl)
    {
        $updateLog = "";        //日志
        $column    = 0;         //文件有多少列 (前10位为固定字段)
        $pringdata = array();   //数据
        $enddata   = array();   //结束段数据
        $base      = 11;        //基础数据行,第一行做提示用
        $str = fopen($fileUrl,"r");
        $row = 0;
        $run = true;            //单行是否需要运行
        while ($data = fgetcsv($str, 1000, ",")) {
            $cityarr= array(); //国家集合
            $run = true; //单行默认运行
            $row++;   
               
            if($row == 1){               
                continue;
            }

            foreach ($data as $k => $d) {              
                if($k != 7 && $k != 1 && ($d === '' || !is_numeric($d))){
                    $updateLog .= "数据错误(数据不能为空或字符),错误行:{$row}<br/>";
                    $run = false;
                    break;
                }
            }

            $cityarr = explode(',', $data[3]);
            if(empty($cityarr)){
                $updateLog .= "数据错误(国家数据不存在),错误行:{$row}<br/>";
                $run = false;
                continue;
            }
            foreach ($cityarr as $k => $city) {
                if($city === '' || !is_numeric($city)){
                    $updateLog .= "数据错误(国家ID不能为空或字符),错误行:{$row}<br/>";
                    $run = false;
                    break;
                }
            }
          
            if($run){
                foreach ($cityarr as $k => $c) {     
                    $expressItem = Application_Model_Express::load(iconv("GB2312", "UTF-8", $data[2]));
                    
                    if(!$expressItem){
                        $updateLog .= "渠道ID错误，无法找到渠道ID为 <font color='#c00;'>".iconv("GB2312", "UTF-8", $data[1])."</font> 的渠道。<br/>";
                        continue;
                    }
                    
                    $cityItem = Application_Model_city::load(iconv("GB2312", "UTF-8", $c));
                    if(!$cityItem){
                        $updateLog .= "国家ID错误，无法找到国家ID为 <font color='#c00;'>".iconv("GB2312", "UTF-8", $c)."</font> 的国家。<br/>";
                        continue;
                    }

                    $priceItem = Application_Model_ExpressPrice::getPriceByExpressAndCity($data[2], $c);   
                                      
                    if($priceItem){
                        $id = $priceItem->Id;
                    }else{                      
                        continue;
                    }
                    $priceItem->FirstPrice = number_format($data[6],2);
                    $priceItem->save(); 

                    $updateLog .= "更新成功，渠道ID为 ".$data[2]." 国家ID为 ".$c." 的<font color='#c00;'>价格记录</font>更新成功。<br/>";
                }
            }
        }
        fclose($str);
        return $updateLog;
    }

    //一次导入
    protected function expressCsv($fileUrl)
    {
        $updateLog = "";        //日志
        $column    = 0;         //文件有多少列 (前10位为固定字段)
        $pringdata = array();   //数据
        $enddata   = array();   //结束段数据
        $base      = 11;        //基础数据行,第一行做提示用
        $str = fopen($fileUrl,"r");
        $row = 0;
        $run = true;            //单行是否需要运行
        while ($data = fgetcsv($str, 1000, ",")) {
            $cityarr= array(); //国家集合
            $run = true; //单行默认运行
            $row++;   
               
            if($row == 1){
                if(count($data) > $base){
                    for ($i = $base; $i < count($data); $i++) { 
                        $tmp    = str_replace('kg','',$data[$i]);
                        $tmparr = explode('~', $tmp);
                        if(count($tmparr) != 2){
                            $updateLog .= 'title错误(数据不完整)<br/>';
                            break 2;
                        }else{
                            if(!is_numeric($tmparr[0]) || !is_numeric($tmparr[1])){
                                $updateLog .= 'title错误(数据类型错误)<br/>';
                                break 2;
                            }else{
                                $enddata[]   = $tmparr[1];
                                $pringdata[] = array(
                                                's' => $tmparr[0], 
                                                'e' => $tmparr[1], 
                                                't' => $tmp == $data[$i] ? '1' : '2',  //1续重2公斤
                                            );
                            }
                        }
                    }
                    //验证
                    if(count($pringdata) > 0){
                        if($pringdata[0]['s'] != 0){
                            $updateLog .= '数据错误(重量段开始必须为0)<br/>';
                            break;
                        }else{
                            if(count($pringdata) > 1){
                                foreach ($pringdata as $k => $p) {
                                    if($k == 0){
                                        continue;
                                    }else{
                                        if($pringdata[$k - 1]['e'] != $p['s']){
                                            $updateLog .= '数据错误(重量段开始必须为上一个结束段)<br/>';
                                            break 2;
                                        }
                                        if($p['e'] <= $p['s']){
                                            $updateLog .= '数据错误(重量段开始必须小于结束段)<br/>';
                                            break 2;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                continue;
            }
   
            if($row == 2){
                //$price = new Application_Model_ExpressPrice();
               // $price->deleteAll(array('expressId' => $data[1], ));
                //$pring = new Application_Model_ExpressPricing();
                //$pring->deleteAll(array('expressId' => $data[1], ));
            }
            
            foreach ($data as $k => $d) {
                if($k != 0 && $k != 2 && ($d === '' || !is_numeric($d)) && !in_array($data[7], $enddata)){
                    $updateLog .= "数据错误(数据不能为空或字符),错误行:{$row}<br/>";
                    $run = false;
                    break;
                }
            }

            $cityarr = explode(',', $data[2]);
            if(empty($cityarr)){
                $updateLog .= "数据错误(国家数据不存在),错误行:{$row}<br/>";
                $run = false;
                continue;
            }
            foreach ($cityarr as $k => $city) {
                if($city === '' || !is_numeric($city)){
                    $updateLog .= "数据错误(国家ID不能为空或字符),错误行:{$row}<br/>";
                    $run = false;
                    break;
                }
            }

            if($run){
                foreach ($cityarr as $k => $c) {
                    $listxz = array(); //临时数据,续重
                    $listgj = array(); //临时数据,公斤
                    $expressItem = Application_Model_Express::load(iconv("GB2312", "UTF-8", $data[1]));
                    if(!$expressItem){
                        $updateLog .= "渠道ID错误，无法找到渠道ID为 <font color='#c00;'>".iconv("GB2312", "UTF-8", $data[1])."</font> 的渠道。<br/>";
                        continue;
                    }
                    
                    $cityItem = Application_Model_city::load(iconv("GB2312", "UTF-8", $c));
                    if(!$cityItem){
                        $updateLog .= "国家ID错误，无法找到国家ID为 <font color='#c00;'>".iconv("GB2312", "UTF-8", $c)."</font> 的国家。<br/>";
                        continue;
                    }
                    $priceItem = Application_Model_ExpressPrice::getPriceByExpressAndCity($data[1], $c);
                    $id        = 0;                     
                    if($priceItem){
                        $id = $priceItem->Id;
                    }
                    $addPriceItem = Application_Model_ExpressPrice::addNewExpressPrice($data[1], $c, $data[3], $data[4], $data[5], $data[6], $data[7], $data[8], $data[9], $data[10], $id);
                    if(!$addPriceItem){
                        if(empty($id)){
                            $updateLog .= '新增失败，无法新增渠道';
                        }else{
                            $updateLog .= '更新失败，无法更新渠道';
                        }
                        $updateLog .= "ID为 <font color='#c00;'>".iconv("GB2312", "UTF-8", $data[1])."</font> 国家ID为 <font color='#c00;'>".iconv("GB2312", "UTF-8", $data[2])."</font> 的数据。<br/>";
                        continue;
                    }
                    if(empty($id)){
                        $updateLog .= "新增成功，渠道ID为 ".$data[1]." 国家ID为 ".$c." 的<font color='#c00;'>价格记录</font>新增成功。<br/>";
                    }else{
                        $updateLog .= "更新成功，渠道ID为 ".$data[1]." 国家ID为 ".$c." 的<font color='#c00;'>价格记录</font>更新成功。<br/>";
                    }
                    //数据分组
                    if(count($pringdata) > 0){
                        foreach ($pringdata as $k => $p) {
                            $tmpdate  = array(
                                        'expressId' => $data[1],    //快递id
                                        'countryId' => $c,    //国家id
                                        'type'      => $p['t'],     //1续重2公斤
                                        'start'     => $p['s'],     //开始重量段
                                        'end'       => $p['e'],     //结束重量段
                                        'price'     => $data[$base + $k],    //价格
                                    );
                            if($p['t'] == 1){
                                $listxz[] = $tmpdate;
                            }else{
                                $listgj[] = $tmpdate;
                            }
                        }
                    }
                    if(!empty($listxz) || !empty($listgj)){
                        //更新价格段
                        $updateLog = $this->dataSave($data[1], $c, $listxz, $listgj, $updateLog);
                    }
                }
            }
        }
        fclose($str);
        return $updateLog;
    }

    /**
     * 导入操作数据
    */
    protected function dataSave($expressId, $countryId, $listxz, $listgj, $updateLog)
    {
        //先排序数据 升序
        usort($listxz, function($a, $b) {
            $al = $a['start'];
            $bl = $b['start'];
            if ($al == $bl)
                return 0;
            return ($al < $bl) ? -1 : 1;
        });

        usort($listgj, function($a, $b) {
            $al = $a['start'];
            $bl = $b['start'];
            if ($al == $bl)
                return 0;
            return ($al < $bl) ? -1 : 1;
        });

        $runxz = true;
        $rungj = true;
        //这里要开始验证数据start
        foreach ($listxz as $key => $value) {
            //开始价格段
            if($key != 0 && (!is_numeric($value['start']) || $value['start'] < 0)){
                $updateLog .= "渠道ID{$expressId},国家ID{$countryId},类型(续重)的价格段数据错误1,<br>";
                $runxz = false;
                break;
            }

            if($key != 0 && $value['start'] != $listxz[$key-1]['end']){
                $updateLog .= "渠道ID{$expressId},国家ID{$countryId},类型(续重)的价格段数据错误2,<br>";
                $runxz = false;
                break;
            }

            //结束价格段
            if(count($listxz) > 2 ){
                // if( count($listxz) != $key + 1 && ( !is_numeric($value['end']) || $value['end'] < 0 || $value['end'] == '' || $value['end'] <= $listxz[$key-1]['end'] )){
                if( count($listxz) != $key + 1 && ( !is_numeric($value['end']) || $value['end'] < 0 || $value['end'] == '')){
                    $updateLog .= "渠道ID{$expressId},国家ID{$countryId},类型(续重)的价格段数据错误3,<br>";
                    $runxz = false;
                    break;
                }
            }else{
                if( count($listxz) != $key + 1 && ( !is_numeric($value['end']) || $value['end'] < 0 || $value['end'] == '')){
                    $updateLog .= "渠道ID{$expressId},国家ID{$countryId},类型(续重)的价格段数据错误4,<br>";
                    $runxz = false;
                    break;
                }
            }

            //价格验证
            // if(!is_numeric($value['price']) || $value['price'] <= 0){
            //     $updateLog .= "渠道ID{$expressId},国家ID{$countryId},类型(续重)的价格段数据错误5,<br>";
            //     $runxz = false;
            //     break;
            // }
        }

        foreach ($listgj as $key => $value) {
            //开始价格段
            if($key != 0 && (!is_numeric($value['start']) || $value['start'] < 0)){
                $updateLog .= "渠道ID{$expressId},国家ID{$countryId},类型(公斤)的价格段数据错误1,<br>";
                $rungj = false;
                break;
            }
            if($key != 0 && $value['start'] != $listgj[$key-1]['end']){
                $updateLog .= "渠道ID{$expressId},国家ID{$countryId},类型(公斤)的价格段数据错误2,<br>";
                $rungj = false;
                break;
            }

            //结束价格段
            if(count($listgj) > 2 ){
                if( count($listgj) != $key + 1 && ( !is_numeric($value['end']) || $value['end'] < 0 || $value['end'] == '' || $value['end'] <= $listgj[$key-1]['end'] )){
                    $updateLog .= "渠道ID{$expressId},国家ID{$countryId},类型(公斤)的价格段数据错误3,<br>";
                    $rungj = false;
                    break;
                }
            }else{
                if( count($listgj) != $key + 1 && ( !is_numeric($value['end']) || $value['end'] < 0 || $value['end'] == '')){
                    $updateLog .= "渠道ID{$expressId},国家ID{$countryId},类型(公斤)的价格段数据错误4,<br>";
                    $rungj = false;
                    break;
                }
            }

            //价格验证
            // if(!is_numeric($value['price']) || $value['price'] <= 0){
            //     $updateLog .= "渠道ID{$expressId},国家ID{$countryId},类型(公斤)的价格段数据(价格)错误5,<br>";
            //     $rungj = false;
            //     break;
            // }
        }
        //这里要开始验证数据end

        if($runxz){
            //1删除数据(删除存在的价格信息)
            $clean = new Application_Model_ExpressPricing();
            $clean->deleteAll(array('expressId' => $expressId, 'countryId' => $countryId, 'type' => Application_Model_ExpressPricing::TYPE_CONTINUED));//删除对应所有信息

            //2加入新数据
            //续重
            $tmpcount   = 0;
            $emptycount = 0;
            foreach ($listxz as $key => $list) {
                if(!empty($list['end']) && !empty($list['price'])){
                    $Pricing = new Application_Model_ExpressPricing();
                    $Pricing->ExpressId = $expressId;
                    $Pricing->CountryId = $countryId;
                    $Pricing->Type      = Application_Model_ExpressPricing::TYPE_CONTINUED;
                    $Pricing->Start     = $list['start'];
                    $Pricing->End       = $list['end'];
                    $Pricing->Price     = $list['price'];
                    $Pricing->save();
                    $tmpcount++;
                }else{
                    $emptycount++;
                }
            }
            if($tmpcount > 0){
                if(count($listxz) == ($tmpcount + $emptycount)){
                    $updateLog .= "更新成功，渠道ID为 ".$expressId." 国家ID为 ".$countryId." 的<font color='#c00;'>续重价格段</font>记录更新成功。<br>";
                }else{
                    $updateLog .= "更新异常，渠道ID为 ".$expressId." 国家ID为 ".$countryId." 的<font color='#c00;'>续重价格段</font>记录更新异常,请查询详细信息。<br>";
                }
            }
        }

        if($rungj){
            //1删除数据(删除存在的价格信息)
            $clean = new Application_Model_ExpressPricing();
            $clean->deleteAll(array('expressId' => $expressId, 'countryId' => $countryId, 'type' => Application_Model_ExpressPricing::TYPE_KG));//删除对应所有信息
            
            //按公斤算
            $tmpcount = 0;
            foreach ($listgj as $key => $list) {
                $Pricing = new Application_Model_ExpressPricing();
                $Pricing->ExpressId = $expressId;
                $Pricing->CountryId = $countryId;
                $Pricing->Type      = Application_Model_ExpressPricing::TYPE_KG;
                $Pricing->Start     = $list['start'];
                $Pricing->End       = $list['end'];
                $Pricing->Price     = $list['price'];
                $Pricing->save();;
                $tmpcount++;
            }
            if($tmpcount > 0){
                if(count($listgj) == $tmpcount){
                    $updateLog .= "更新成功，渠道ID为 ".$expressId." 国家ID为 ".$countryId." 的<font color='#c00;'>公斤价格段</font>记录更新成功。<br>";
                }else{
                    $updateLog .= "更新异常，渠道ID为 ".$expressId." 国家ID为 ".$countryId." 的<font color='#c00;'>公斤价格段</font>记录更新异常,请查询详细信息。<br>";
                }
            }
        }
        //返回运行结果
        return $updateLog;
    }

    /**
     * 原数据
     * 表格数据
     * 数据类型1续重2公斤
     * @return list
    */
    protected function saveList($list, $data, $type = 1)
    {
        $type = $type == 1 ? 1 : 2;
        $list[]  = array(
                    'expressId' => $data[0],    //快递id
                    'countryId' => $data[1],    //国家id
                    'type'      => $type,           //1续重2公斤
                    'start'     => $data[3],    //开始重量段
                    'end'       => $data[4],    //结束重量段
                    'price'     => $data[5],    //价格
                );
        return $list;
    }

    //vip价格添加or更新
    public function vipAction()
    {
        $id      = (int)$this->getSafeParam("id");
        $express = Application_Model_Express::load($id);

        if(empty($express)){
            $this->_redirect('admini/express/index');
        }else{
            $vip     = Application_Model_ExpressVip::getAll(array('expressId' => $id ));
            if($vip){
                $this->setTitle("更新VIP优惠设置");
            }else{
                $this->setTitle("VIP优惠设置");
            }

            $this->view->vip     = $vip;
            $this->view->express = $express;
        }
    }

    //更新或添加
    public function vipsaveAction()
    {
        $re = array(
                's' => -1, 
                'm' => '', 
            );
        $id = $this->getSafeParam('id');
        $express = Application_Model_Express::load($id);

        if(empty($express)){
            $re = array(
                's' => 41, 
                'm' => '快递不存在', 
            );
        }else{
            $data = array(
                      's1' => (float)$this->getSafeParam('s1'),
                      's2' => (float)$this->getSafeParam('s2'),
                      's3' => (float)$this->getSafeParam('s3'),
                      's4' => (float)$this->getSafeParam('s4'),
                      'x1' => (float)$this->getSafeParam('x1'),
                      'x2' => (float)$this->getSafeParam('x2'),
                      'x3' => (float)$this->getSafeParam('x3'),
                      'x4' => (float)$this->getSafeParam('x4'),
                      'g1' => (float)$this->getSafeParam('g1'),
                      'g2' => (float)$this->getSafeParam('g2'),
                      'g3' => (float)$this->getSafeParam('g3'),
                      'g4' => (float)$this->getSafeParam('g4'),
                    );
            foreach ($data as $k => $d) {
                if($d > 10 || $d <= 0){
                    $data[$k] = 10;
                }
            }
            $run  = true;
            for ($i=1; $i <= 3 && $run ; $i++) { 
                switch ($i) {
                    case 1:
                        $prefix = 's';
                        $type = Application_Model_ExpressVip::TYPE_YKG;
                        break;
                    case 2:
                        $prefix = 'x';
                        $type = Application_Model_ExpressVip::TYPE_CONTINUED;
                        break;
                    case 3:
                        $prefix = 'g';
                        $type = Application_Model_ExpressVip::TYPE_KG;
                        break;
                    default:
                        $run    = false;
                        break;
                }
                if($run){
                    $info     = Application_Model_ExpressVip::getInfo(array('expressId' => $id , 'type' => $type ));
                    $vip = new Application_Model_ExpressVip();
                    if(!empty($info)){
                        $vip->id        = $info->id;
                    }
                    $vip->expressId = $id;
                    $vip->type      = $type;
                    $vip->advanced  = $data[$prefix.'1'];
                    $vip->gold      = $data[$prefix.'2'];
                    $vip->platinum  = $data[$prefix.'3'];
                    $vip->jewel     = $data[$prefix.'4'];
                    $vip->save();
                }
            }
            $re = array(
                's' => 0, 
                'm' => '设置成功', 
            );
        }

        echo json_encode($re);
        exit;
    }

    //价格列表显示
    public function vipviewAction()
    {
        $id      = (int)$this->getSafeParam("id");
        $express = Application_Model_Express::load($id);

        if(empty($express)){
            $this->_redirect('admini/express/index');
        }else{
            $vip     = Application_Model_ExpressVip::getAll(array('expressId' => $id ));
            if(!$vip){
                $this->_redirect("admini/express/vip/id/{$id}");
            }else{
                $this->setTitle("VIP优惠详情");
                $this->view->vip = $vip;
                $this->view->id  = $id;
            }
        }
    }

    /**
     * 删除vip价格设置
    */
    public function vipdelAction()
    {
        $id      = (int)$this->getSafeParam("id");
        $vip = new Application_Model_ExpressVip();
        $vip->deleteAll(array('expressId' => $id));
        $re = array(
                's' => 0, 
                'm' => '删除成功', 
                'd' => $id,
            );
        echo json_encode($re);
        exit;
    }
    //从递四方获取首重价
    public function batchdownAction(){
        $express='';
        $expressId = (int)$this->getSafeParam("id");
        if($expressId==1){
            $express='联邦';
        }elseif ($expressId==2) {
           $express='DHL';
        }else{
            exit();
        }

        $cityItem = Application_Model_city::byAllcity(array('parentId' => 0), 'id');
        $arrs=array();
        $rep=Uuch_4pxAPI::ChargeCalculateServer($cityItem,0.5,$expressId);
       
        $er = new Application_Model_Filter_GetdataDump();
        $er->title = $express."快递价格维护";
        $er->printHeadArr = array('','国家名称(提示用)', '渠道ID', '国家ID', '首重(g)', '续重(g)', '首重价');
       
        $er->dumpDataTop();
        $er->dumoArrData($rep);
        exit();
    }
}
